<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Model\Cards;
use App\Model\UserBonusCount;
use App\Model\UserCommissionDetail;
use App\Model\UserInviteOneYuan;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Swagger\Annotations as SWG;

class MemberController extends Controller
{
	/**
	 * 获取推荐用
	 * @param Request $request
	 * @return \Illuminate\Http\Response
	 */
	public function getPromUser(Request $request)
	{
		$relation_id = $request->user()->id;
		$user = User::where('id', $relation_id)->first();
		$type = $request->input('type', 1);
		$data = $user->getUserFrom($relation_id, $user->type, $type);

		$items = [];
		foreach ($data as $datum) {
			$items[] = [
				'id' => $datum['id'],
				'name' => User::getUserName($datum['user_id']),
				'create_at' => $datum['created_at']->format('Y-m-d H:i:s')
			];
		}

		return $this->renderJson($items);
	}

	/**
	 * 获取推荐用户和佣金
	 * @param Request $request
	 * @return \Illuminate\Http\Response
	 */
	public function getPromUserAmount(Request $request)
	{
		$relation_id = $request->user()->id;
		// 用户自己
		$user = User::where('id', $relation_id)->first();
		$type = $request->input('type');
		$data = $user->getUserFromAmount($relation_id, $user->type, $type);
		$items = [];
		foreach ($data as $datum) {
			$items[] = [
				'user_name' => User::getUserName($datum['user_id']),
				'money' => $datum['money'],
				'created_at' => $datum['created_at']->format('Y-m-d H:i:s')
			];
		}

		$totalAmount = $user->currentCommission;
		$changeCommission = $user->changeCommission;

		return $this->renderJson(['items' => $items, 'totalAmount' => $totalAmount, 'changeCommission' => $changeCommission]);
	}

    /**
     * @SWG\Get(
     *     path="/front/reward/bill",
     *     tags={"奖励金"},
     *     summary="奖励金明细",
     *     description="奖励金明细",
     *     @SWG\Parameter(ref="#/parameters/auth"),
     *     @SWG\Response(
     *          response="200",
     *          description="ok",
     *          @SWG\Schema(
     *              @SWG\Property(property="current", type="integer", description="当前奖励金"),
     *              @SWG\Property(property="total", type="integer", description="总奖励金"),
     *              @SWG\Property(
     *                  property="data",
     *                  type="array",
     *                  @SWG\Items(
     *                      @SWG\Property(property="type", type="integer", description="类型：1)奖励金获取,2)拉新,3)转入余额,4)注册送钱"),
     *                      @SWG\Property(property="income_type", type="integer", description="类型1：收入，0支出"),
     *                      @SWG\Property(property="money", type="integer", description="钱"),
     *                      @SWG\Property(property="describe", type="string", description="描述"),
     *                      @SWG\Property(property="user",
     *                           type="object",
     *                           description="1和4时有值，相关用户",
     *                           @SWG\Property(property="id", type="integer", description="用户id"),
     *                           @SWG\Property(property="name", type="string", description="用户名"),
     *                      ),
     *                  )
     * )
     *          )
     *      )
     * )
     */
	public function bill()
	{
		$user = Auth::user();
		$data['current'] = $user->currentCommission;
		$data['total'] = $user->rewards_all;
		if ($user->type == 4) {
			$page = UserBonusCount::whereUserId($user->id)->select('type', 'income_type', 'money', 'describe', 'created_at', 'relation_id')->orderByDesc('id')->paginate();
			$items = $page->items();
			foreach ($items as &$item){
                $item->user = [
                    'id' => 0,
                    'name' => ''
                ];
			    if ($item->type == 1){
			        //分享提成
                    $c = UserCommissionDetail::find($item->relation_id);
                    if (!$c) {
                        continue;
                    }
                    $u = [];
                    $u['id'] = $c->user_id;
                    $u['name'] = Cards::where('user_id', $c->user_id)->value('user_name');
                    $item->user = $u;
                }elseif($item->type == 2){
			        //一元拉新
                    $y = UserInviteOneYuan::find($item->relation_id);
                    if (!$y) {
                        continue;
                    }
                    if ($y->user_id == Auth::id()) {
                        //注册
                        $item->type = 4;
                        continue;
                    }
                    $u['id'] = $y->user_id;
                    $u['name'] = Cards::where('user_id', $y->user_id)->value('user_name');
                    $item->user = $u;
                }
            }
			$page = $page->toArray();
			$page['data'] = $items;
			$data['list'] = $page;
		}
		return $this->renderJson($data);
	}
}
